package com.xueke.crawler.service.initTree;

import com.xueke.crawler.helper.HttpHelper;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weibo on 2017/5/13.
 *
 * @author weibo.qin
 * @version 2017/5/13
 */

@Component("sxInitTree")
public class SxInitTree extends BaseInitTree {

    @Override
    public void start() {
        List<SubjectSummary> list = initSubjectSummary();

        for (SubjectSummary subjectSummary : list) {
            System.out.println(subjectSummary.getUrl());
            handleClassNode(subjectSummary.getUrl(), subjectSummary.getDomain(), subjectSummary.getCid());
        }
    }


    public List<SubjectSummary> initSubjectSummary() {

        List<SubjectSummary> list = new ArrayList<>();

        //yw
        list.add(new SubjectSummary("http://yw.zxxk.com/", "http://yw.zxxk.com/", "13"));
        list.add(new SubjectSummary("http://yw.zxxk.com/m/", "http://yw.zxxk.com/", "13"));

        //yw
        list.add(new SubjectSummary("http://wl.zxxk.com/", "http://wl.zxxk.com/", "11"));
        list.add(new SubjectSummary("http://wl.zxxk.com/m/", "http://wl.zxxk.com/", "11"));

        //yw
        list.add(new SubjectSummary("http://sx.zxxk.com/", "http://sx.zxxk.com/", "12"));
        list.add(new SubjectSummary("http://sx.zxxk.com/m/", "http://sx.zxxk.com/", "12"));

        //yw
        list.add(new SubjectSummary("http://yy.zxxk.com/", "http://yy.zxxk.com/", "14"));
        list.add(new SubjectSummary("http://yy.zxxk.com/m/", "http://yy.zxxk.com/", "14"));

        //yw
        list.add(new SubjectSummary("http://hx.zxxk.com/", "http://hx.zxxk.com/", "15"));
        list.add(new SubjectSummary("http://hx.zxxk.com/m/", "http://hx.zxxk.com/", "15"));

        //yw
        list.add(new SubjectSummary("http://sw.zxxk.com/", "http://sw.zxxk.com/", "16"));
        list.add(new SubjectSummary("http://sw.zxxk.com/m/", "http://sw.zxxk.com/", "16"));

        //yw
        list.add(new SubjectSummary("http://zz.zxxk.com/", "http://zz.zxxk.com/", "17"));
        list.add(new SubjectSummary("http://zz.zxxk.com/m/", "http://zz.zxxk.com/", "17"));

        //yw
        list.add(new SubjectSummary("http://ls.zxxk.com/", "http://ls.zxxk.com/", "18"));
        list.add(new SubjectSummary("http://ls.zxxk.com/m/", "http://ls.zxxk.com/", "18"));

        //yw
        list.add(new SubjectSummary("http://dl.zxxk.com/", "http://dl.zxxk.com/", "19"));
        list.add(new SubjectSummary("http://dl.zxxk.com/m/", "http://dl.zxxk.com/", "19"));

        //yw
        list.add(new SubjectSummary("http://kx.zxxk.com/", "http://kx.zxxk.com/", "20"));
        list.add(new SubjectSummary("http://xx.zxxk.com/", "http://xx.zxxk.com/", "21"));

        return list;
    }


    static class SubjectSummary {
        private String url;
        private String domain;
        private String cid;

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getDomain() {
            return domain;
        }

        public void setDomain(String domain) {
            this.domain = domain;
        }

        public String getCid() {
            return cid;
        }

        public void setCid(String cid) {
            this.cid = cid;
        }

        public SubjectSummary(String url, String domain, String cid) {
            this.url = url;
            this.domain = domain;
            this.cid = cid;
        }
    }

    private void handleClassNode(String url, String domain, String cid) {
        Document doc = HttpHelper.getSimpleDocument(url);

        Elements elements = doc.select("div.nav-det a");
        for (Element e : elements) {
            String tmpUrl = e.attr("href");
            System.out.println(tmpUrl + "----------------------");
            String dataType = checkDataType(tmpUrl);
            String channelId = tmpUrl.substring(tmpUrl.indexOf(dataType) + dataType.length(), tmpUrl.lastIndexOf("/"));
            String title = e.text();
            System.out.println("handleClassNode -----> " + tmpUrl + "-------" + title + "-------" + channelId +
                "--------" +
                dataType);
            saveSubjectTree(cid, channelId, domain, tmpUrl, title, channelId,
                0, dataType);
            handleChapterNode(cid, channelId, domain, tmpUrl);
        }
    }

    private void handleChapterNode(String cid, String pid, String domain2,
                                   String tmpUrl) {
        Document doc = HttpHelper.getSimpleDocument(domain2 + tmpUrl);
        Elements elements = doc.select("li.tree-li");
        for (Element e : elements) {
            String chapterNode = e.select("a:eq(0)").attr("href");
            System.out.println(chapterNode);
            String chapterDataType = checkDataType(chapterNode);
            String chapterId = chapterNode.substring(chapterNode.indexOf(chapterDataType)+chapterDataType.length(),chapterNode.lastIndexOf("/"));
            String chapterTitle = e.select("a:eq(0)").attr("title");
            int isleaf = 0;
            if ("sec".equalsIgnoreCase(chapterDataType)) {
                isleaf = 1;
            }
            System.out.println("handleChapterNode -----> " + chapterNode + "-------" + chapterTitle + "-------" +
                chapterId + "--------" +
                chapterDataType);

            saveSubjectTree(cid, chapterId, domain2, chapterNode, chapterTitle, pid,
                isleaf, chapterDataType);

            Elements lis = e.select("ul li a");

            if (isleaf == 0) {
                pid = chapterId;
            }

            for (Element li : lis) {
                String url = li.attr("href");
                System.out.println(url);
                String dataType = checkDataType(url);
                String nodeId = url.substring(url.indexOf(dataType) + dataType.length(), url.lastIndexOf("/"));
                String title = li.text();
                isleaf = 1;
//                isleaf = 0;
//                if ("node".equalsIgnoreCase(dataType)) {
//                    isleaf = 1;
//                }
                System.out.println("handleLeafNode -----> " + url + "-------" + title + "-------" + nodeId + "--------" +
                    dataType);

                saveSubjectTree(cid, nodeId, domain2, url, title, pid,
                    isleaf, dataType);
            }


        }
    }

	private String checkDataType(String chapterNode) {
		if(chapterNode.contains("book")) return "book";
		if(chapterNode.contains("chapter")) return "chapter";
		if(chapterNode.contains("sec")) return "sec";
		return null;
	}

}
